
import { _decorator, Component, Node, Prefab, instantiate, UITransform, Vec2, math } from 'cc';
import { GameConfig } from '../gameConfig';
const { ccclass, property } = _decorator;
 
@ccclass('FogManager')
export class FogManager extends Component {
    @property(Prefab)
    public fogPre: Prefab = null;
    
    public n: number = 19;
    public fogMetaArr: number[] = null; //迷雾，0表示该区域有迷雾，0xff表示没有
    public fogArr: Node[] = null;

    static HIDE_MASK: number = 0; //迷雾笼罩
    static BARE_MASK: number = 0xff; //迷雾被驱散

    init () {
        this.n = GameConfig.instance.gameSize;
        let leftDev = this.n / 2 - 1;
        let width = Math.floor(GameConfig.instance.width);
        this.fogArr = new Array<Node>();
        for (let i = 0 ; i < this.n; i++) {
            for (let j = 0; j < this.n; j++) {
                let fog = instantiate(this.fogPre);
                fog.name = "fog" + i + j;
                let uiTransform = fog.getComponent(UITransform);
                uiTransform.contentSize = new math.Size(width, width);
                uiTransform.anchorPoint = new Vec2(i - leftDev, j - leftDev);
                console.log(uiTransform);
                this.node.addChild(fog);           
                this.fogArr.push(fog);
            }
        }
        this.fogArr.reverse();
    }

    stroke() {
        for  (let i = 0 ; i < this.fogMetaArr.length; i++) {
            if (this.fogMetaArr[i] == FogManager.BARE_MASK) {
                this.fogArr[i].active = false;
            }
        }
    }

    setfogArrActive(v: Vec2, active: boolean) {
        this.fogArr[v.x * GameConfig.instance.gameSize + v.y].active = true;
    }
}
